#pragma once

#include <iostream>
#include <string>
#include <cstdio>
#include <functional>

#define NUM 1024

// serviceIO 这个函数写在这里也是没有问题的
void serviceIO(int sock){
    char buffer[NUM];
    while(true){
        ssize_t n = read(sock, buffer, sizeof(buffer) - 1);
        if(n > 0){
            buffer[n] = 0;
            std::cout << "recv message: " << buffer << std::endl;

            std::string message = buffer;
            message += " server[echo]";

            write(sock, message.c_str(), message.size());
        }
        else if(n == 0){
            // 代表 client 退出了
            // logMessage(NORMAL, "client quit, me too!");
            std::cout << "client quit, me too!" << std::endl;
            break;
        }
    }

    close(sock);
}   


class Task{

    using func_t = std::function<void(int)>;

public:
    Task(){}

    Task(int sock, func_t func)
        : _sock(sock), _callback(func)
    {}

    void operator()(){
        _callback(_sock);
    }

private:
    int _sock;
    func_t _callback;
};